package struct.scene.ui
{
	import collection.DataCollection;
	
	import com.common.displayObjects.Component;
	import com.common.displayObjects.ComponentChildren;
	import com.common.utils.Clock;
	
	import data.MapVO;
	import data.PlayerVO;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import struct.gameinfo.GameInfoModule;
	import struct.scene.SceneModule;
	import struct.scene.items.Grid;
	import struct.scene.items.Player;
	
	import utils.Alert;
	
	/**
	 * 场景的 角色层 
	 * @author Administrator
	 * 
	 */	
	public class ScenePlayerLayer extends ComponentChildren
	{
		public function ScenePlayerLayer(width:int, height:int)
		{
			super(width, height);
		}
		
		private var timer:Timer;
		override protected function createChildren():void
		{
			super.createChildren();
			arrPlayer = new Array();
			
			timer = new Timer(100, 0xffffff);
			timer.addEventListener(TimerEvent.TIMER, excution);
			timer.start();
		}
		
		protected function excution(event:TimerEvent):void
		{
			var player:Player;
			if (!DataCollection.gameStart){
				//游戏已经暂停
				for (var j:int; j< arrPlayer.length; j++)
				{
					player = arrPlayer[j] as Player;
					player.stop();
				}
				return;
			}
			for (var i:int; i< arrPlayer.length; i++)
			{
				player = arrPlayer[i] as Player;
				player.updatePosition(); //player在移动  更新player移动后的信息
				//TODO: 检查user是否和其他AI碰撞
				SceneModule.getInstance().scene.sceneGridlayer.checkItems(player); //player和item的碰撞检测
				if (player.isUser)
				{
					//用户控制的角色
					SceneModule.getInstance().scene.scenePlayerlayer.checkPlayers(player); //player和item的碰撞检测
					
					if (player.boUpDown || player.boRightDown || player.boDownDown || player.boLeftDown) {
						// 方向键有优先级 
						var direction:int;
						if (player.boRightDown)
						{
							direction = 2;
						}
						else if (player.boLeftDown)
						{
							direction = 8;
						}
						else if (player.boUpDown)
						{
							direction = 1;
						}
						else if (player.boDownDown)
						{
							direction = 4;
						}
						var g:Grid = SceneModule.getInstance().scene.sceneGridlayer.getEdgeGrid(player.xIndex, player.yIndex, direction);
						if (g)player.move(g); //移动向目标格子
					}else
					{
						player.stop(); //无按键按下   停止
					}
				}else
				{
					//不是用户控制的角色, AI
					if (player.isMoving)return;
					else{
						var nextGrid:Grid = SceneModule.getInstance().scene.sceneGridlayer.getAINextGrid(player.xIndex, player.yIndex)
						while(!nextGrid)
						{
							nextGrid = SceneModule.getInstance().scene.sceneGridlayer.getAINextGrid(player.xIndex, player.yIndex)
						}
						player.move(nextGrid);
					}
				}
			}
		}
		
		private var arrPlayer:Array;
		private var userPlayer:Player; //用户控制的角色
		
		public function initPlayerLayer(arrPlayerPosition:Array, mapVO:MapVO):void
		{
			// arr中的数据内容 {id:"1001", positionIndex:'24'}
			// id role配置表中的player的编号, 1001为玩家角色, 其他为敌人
			//positionIndex	这个player在地格中的index
			
			for (var i:int; i<arrPlayerPosition.length; i++)
			{
				var o:Object = arrPlayerPosition[i];
				var playerVO:PlayerVO = (DataCollection.hmRoles.get(o.id) as PlayerVO).clone();
				var b:Boolean = i == 0 ? true:false;
				var player:Player = new Player(playerVO, b);
				if (b) userPlayer = player;
				
				player.xIndex =  o.positionIndex%mapVO.columnNum;
				player.yIndex = ( o.positionIndex/mapVO.columnNum) >>0;
				player.x = mapVO.gridWidth * player.xIndex + mapVO.gridWidth * .5;
				player.y = mapVO.gridHeight * player.yIndex + mapVO.gridHeight * .5;
				addChild(player);
				arrPlayer.push(player);
			}
			
		}
		
		public function checkPlayers(userPlayer:Player):void
		{
			for (var i:int; i< arrPlayer.length; i++)
			{
				var p:Player = arrPlayer[i];
				if (p != userPlayer)
				{
					var distance:Number = Point.distance(new Point(p.x, p.y), new Point(userPlayer.x, userPlayer.y));
					if (distance < (p.r + userPlayer.r))
					{
						//被怪碰到  命 -1
						DataCollection.gameStart = false; //暂停游戏
						if (--DataCollection.LIFE > 0)
						{
							removeAllPlayers();
							initPlayerLayer(SceneModule.getInstance().mapVO.arrPlayerPosition, SceneModule.getInstance().mapVO);
							onRestart();
						}else
						{
							Alert.showAlert("GAME OVER");
						}
						GameInfoModule.getInstance().base.resetLife();
					}
				}
			}
		}
		
		public function removeAllPlayers():void
		{
			for (var i:int; i< arrPlayer.length; i++)
			{
				(arrPlayer[i] as Component).dispose();
				removeChild(arrPlayer[i] as Component);
			}
			arrPlayer = [];
		}
		protected function onRestart():void
		{
			DataCollection.gameStart = true;
		}
		override public function dispose():void
		{
			super.dispose();
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, excution);
		}
	}
}